package com.founder.gateway.oauth.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.founder.gateway.oauth.dicts.OrgLevel;
import com.founder.gateway.oauth.orm.orcl.bean.entity.OrgOrganizationEntity;
import com.founder.gateway.oauth.service.OrgService;
import com.founder.gateway.system.bean.CacheStructure;

@Service
public class OrgServiceImpl implements OrgService {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 
	 * @Title: getGxdwZdmc
	 * @Description: 获取管辖单位字段名称：计算在topOrgCode范围内（包括topOrgCode）的管辖单位字段名称，如果gxdw不在topOrgCode内，返回空
	 * @param @param topOrgCode 顶级部门代码
	 * @param @param gxdw 需要计算的管辖单位
	 * @param @return    设定文件
	 * @return String    返回类型
	 * @throw
	 */
	@Override
	@Cacheable(value = CacheStructure.FOUNDER_GATEWAY_API_CACHE, key = "'ORG_getGxdwZdmc_'+#gxdw")
	public String getGxdwZdmc(String gxdw){
		return getGxdwZdmc(null,gxdw);
	}
	
	@Override
	@Cacheable(value = CacheStructure.FOUNDER_GATEWAY_API_CACHE, key = "'ORG_getGxdwZdmc_'+#topOrgCode+'_'+#gxdw")
	public String getGxdwZdmc(String topOrgCode,String gxdw){
		OrgOrganizationEntity gxdwOrg = queryByOrgCode(gxdw);
		if(gxdwOrg == null) {
			return null;
		}
		
		//如果传了顶级部门代码（topOrgCode），则计算的管辖就必须在这个范围内
		if(topOrgCode!=null && !isParentOrg(topOrgCode,gxdwOrg)) {
			return null;
		}
		
		//根据级别返回对应的管辖字段名称
		if(OrgLevel.SJ.equals(gxdwOrg.getOrglevel())) {
			return OrgLevel.GXZDMC_SJ;
		}
		
		if(OrgLevel.FXJ.equals(gxdwOrg.getOrglevel()) || OrgLevel.ZD.equals(gxdwOrg.getOrglevel())) {
			return OrgLevel.GXZDMC_FXJ;
		}
		
		if(OrgLevel.PCS.equals(gxdwOrg.getOrglevel()) || OrgLevel.DD.equals(gxdwOrg.getOrglevel())) {
			return OrgLevel.GXZDMC_PCS;
		}
		
		if(OrgLevel.ZRQ.equals(gxdwOrg.getOrglevel())) {
			return OrgLevel.GXZDMC_ZRQ;
		}
		
		return null;
	}
	
	/**
	 * 
	 * @Title: isParentOrg
	 * @Description: 是否是上级机构
	 * @param @param parentOrgCode 上级机构代码
	 * @param @param childOrg 下级机构
	 * @param @return    设定文件
	 * @return boolean    返回类型
	 * @throw
	 */
	@Override
	public boolean isParentOrg(String parentOrgCode,OrgOrganizationEntity childOrg) {
		List<OrgOrganizationEntity> orgList = queryParentOrgList(childOrg.getId());
		if(orgList==null || orgList.isEmpty())	return false;
		for(OrgOrganizationEntity org:orgList) {
			if(parentOrgCode.equals(org.getOrgcode())) {
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * 
	 * @Title: queryParentOrgList
	 * @Description: 查询上级单位列表（包括本身）
	 * @param @param orgid 组织机机构ID
	 * @param @return    设定文件
	 * @return List<OrgOrganizationEntity>    返回类型
	 * @throw
	 */
	@Override
	@Cacheable(value = CacheStructure.FOUNDER_GATEWAY_API_CACHE, key = "'ORG_queryParentOrgList_'+#orgid")
	public List<OrgOrganizationEntity> queryParentOrgList(String orgid){
		OrgOrganizationEntity org = new OrgOrganizationEntity();
		return org.executeForList("select * from ORG_ORGANIZATION where XT_ZXBZ = '0' connect by ID = prior PARENTID start with XT_ZXBZ = '0' and ID = ?", orgid);
	}
	
	/**
	 * 
	 * @Title: queryByOrgCode
	 * @Description: 根据组织机构代码查询组织机构对象
	 * @param @param orgCode 组织机构的代码
	 * @param @return    设定文件
	 * @return OrgOrganizationEntity    返回类型
	 * @throw
	 */
	@Override
	@Cacheable(value = CacheStructure.FOUNDER_GATEWAY_API_CACHE, key = "'ORG_queryByOrgCode_'+#orgCode")
	public OrgOrganizationEntity queryByOrgCode(String orgCode){
		try {
			OrgOrganizationEntity org = new OrgOrganizationEntity();
			org.setXtZxbz("0");
			org.setOrgcode(orgCode);
			org.loadBySelf();
			return org;
		}catch(Exception e) {
			logger.error(e.getMessage());
			return null;
		}
	}
}
